/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       button_group.cpp

	$Header: /game/button_group.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "button_group.h"

#include "adaptor_handler.h"
#include "bound_handler.h"

// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// -----------------------------------------------------------------
void t_button_group::add( t_toggle_button* button )
{
	// add a hook so we are notified when the button is clicked.
	t_button_handler function = button->get_click_handler();

	button->set_click_handler( add_2nd_argument( 
		bound_handler( *this, &t_button_group::clicked ), function) );

	m_buttons.push_back( button );
}

// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// -----------------------------------------------------------------
void t_button_group::clicked( t_button* button, t_button_handler function )
{
	if (button->is_pressed())
	{
		std::vector<t_toggle_button_ptr>::iterator index;

		for (index = m_buttons.begin(); index != m_buttons.end(); index++)
			(*index)->set_pressed( index->get() == button );
	}
	function( button );
}

// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// Enable / disable all buttons
// -----------------------------------------------------------------
void t_button_group::enable( bool enable_button )
{
	std::vector<t_toggle_button_ptr>::iterator it;

	for ( it = m_buttons.begin(); it != m_buttons.end(); it++ )
	{
		(*it)->enable( enable_button );
	}
}

// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// Enable / disable 1 button
// -----------------------------------------------------------------
void t_button_group::enable( int id, bool enable_button )
{
	m_buttons[id]->enable( enable_button );
}


// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// -----------------------------------------------------------------
void t_button_group::select( int id )
{
	if ( id < 0 || id >= m_buttons.size())
		return;
	m_buttons[id]->set_pressed();
}

// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// Set 1 button visible or invisible
// -----------------------------------------------------------------
void t_button_group::set_visible( int id, bool visible )
{
	m_buttons[id]->set_visible( visible );
}

// -----------------------------------------------------------------
// group of toggle buttons, only 1 of which can be pressed
// Set all the buttons visible or invisible
// -----------------------------------------------------------------
void t_button_group::set_visible( bool visible )
{
	std::vector<t_toggle_button_ptr>::iterator it;

	for ( it = m_buttons.begin(); it != m_buttons.end(); it++ )
	{
		(*it)->set_visible( visible );
	}
}

